package com.redwine.app.chat;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.exceptions.HyphenateException;
import com.redwine.app.R;
import com.redwine.app.chat.db.DBManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试的类，等使用完在删除霍永刚
 */
public class TestActivity extends Activity {

    private static final String TAG = "LoginActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        register();
        DBManager.getInstance().closeDB();
        // reset current user name before login
        List<EaseUser> aContactList = new ArrayList<EaseUser>();
        EaseUser easeUser = new EaseUser("gassion");
        easeUser.setAvatar("http://img5.duitang.com/uploads/item/201508/30/20150830132007_TjANX.thumb.224_0.jpeg");
        easeUser.setNickname("hello");
        aContactList.add(easeUser);
        EaseUser easeUsera = new EaseUser("1860005761");
        easeUsera.setAvatar("http://img5.duitang.com/uploads/item/201508/30/20150830132007_TjANX.thumb.224_0.jpeg");
        easeUsera.setNickname("hello");
        aContactList.add(easeUsera);
        ChatHelper.getInstance().updateContactList(aContactList);
        /**
         * 设置本地头像和昵称
         */
        ChatHelper.getInstance().getUserProfileManager().setCurrentUserAvatar("http://img5.duitang.com/uploads/item/201508/30/20150830132007_TjANX.thumb.224_0.jpeg");
        ChatHelper.getInstance().getUserProfileManager().setCurrentUserNick("hello");

        ChatHelper.getInstance().setCurrentUserName("18600057650");
        EMClient.getInstance().login("18600057650", "123456", new EMCallBack() {

            @Override
            public void onSuccess() {
                Log.d(TAG, "login: onSuccess");
                // ** manually load all local groups and conversation
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();
            }

            @Override
            public void onProgress(int progress, String status) {
                Log.d(TAG, "login: onProgress");
            }

            @Override
            public void onError(final int code, final String message) {
                Log.d(TAG, "login: onError: " + code);
            }
        });
    }

    public void register() {
        new Thread(new Runnable() {
            public void run() {
                try {
                    // call method in SDK
                    EMClient.getInstance().createAccount("霍永刚", "123456");
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(getApplicationContext(), getResources().getString(R.string.Registered_successfully), Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    });
                } catch (final HyphenateException e) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            int errorCode = e.getErrorCode();
                            if (errorCode == EMError.NETWORK_ERROR) {
                                Toast.makeText(getApplicationContext(), getResources().getString(R.string.network_anomalies), Toast.LENGTH_SHORT).show();
                            } else if (errorCode == EMError.USER_ALREADY_EXIST) {
                                Toast.makeText(getApplicationContext(), getResources().getString(R.string.User_already_exists), Toast.LENGTH_SHORT).show();
                            } else if (errorCode == EMError.USER_AUTHENTICATION_FAILED) {
                                Toast.makeText(getApplicationContext(), getResources().getString(R.string.registration_failed_without_permission), Toast.LENGTH_SHORT).show();
                            } else if (errorCode == EMError.USER_ILLEGAL_ARGUMENT) {
                                Toast.makeText(getApplicationContext(), getResources().getString(R.string.illegal_user_name), Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getApplicationContext(), getResources().getString(R.string.Registration_failed), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        }).start();

    }

    /**
     * if ever logged in
     *
     * @return
     */
    public boolean isLoggedIn() {
        return EMClient.getInstance().isLoggedInBefore();
    }


    public void logOut() {
        ChatHelper.getInstance().logout(false, new EMCallBack() {

            @Override
            public void onSuccess() {
                runOnUiThread(new Runnable() {
                    public void run() {

                    }
                });
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {

            }
        });
    }

    /**
     * 请求网络获取注册的用户信息
     * <p>
     * 头像以及昵称
     */
    private void requestEaseUser() {
        DBManager.getInstance().closeDB();
        List<EaseUser> aContactList = new ArrayList<EaseUser>();
        EaseUser easeUser = new EaseUser("gassion");
        easeUser.setAvatar("http://img5.duitang.com/uploads/item/201508/30/20150830132007_TjANX.thumb.224_0.jpeg");
        easeUser.setNickname("hello");
        aContactList.add(easeUser);
        ChatHelper.getInstance().updateContactList(aContactList);
    }
}
